import { EnumUserType } from '../../utils/constant.js'
const app = getApp(),url = app.globalData.url;
Page({
  data: {
    type:'',
    isQiYe: false,
    searchValue: '',
    dataList: [],   // 搜索结果 
    search:{
      pageNum: 1,
      pageSize: 10
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.userInfo = wx.getStorageSync('userInfo')
    let userType = wx.getStorageSync('userType')
    let isQiYe = userType === EnumUserType.QiYe
    this.setData({ 
      isQiYe,
      type: isQiYe ? '' : 'position'
    })
  },
  select(e){
    var type = e.currentTarget.dataset.type
    this.setData({type})
  },
  setSearchValue(e){
    this.setData({
      searchValue: e.detail.value
    })
  },
  //回车搜索 //1 企业列表  2 职位列表
  searchConfirm(){
    this.setData({
      dataList: [],
      'search.pageNum': 1,
    })
    this.onSearch()
  },
  onSearch(){
    if(!this.data.searchValue){
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
      return
    }
    if(this.data.isQiYe){
      this.getRCList()
      return
    }
    if(this.data.type == 'position'){
      this.getPositionList()
      return
    }
    this.getQiYeList()
  },
  getQiYeList(){
    let params = {
      companyName: this.data.searchValue,
      currentPage: this.data.search.pageNum,
      industry: "",
      nature: "",
      pageSize: this.data.search.pageSize,
      size: "",
    }
    this.setData({ isLoading: true })
    let _this = this
    wx.request({
      url: url+ 'enterpriselib/getEnterpriseLibList',
      method: "POST",
      header:{ 'Content-Type': 'application/json',},
      data: params,
      success(res){
        _this.setData({ isLoading: false })
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: resData.msg || "搜索企业出错",
            icon:'error'
          })
          return
        }
        let dataList = (resData.data.content || []).filter(item => item.id) 
        
        if(_this.data.search.pageNum > 1 && dataList.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        _this.setData({
          dataList: [..._this.data.dataList, ...dataList]
        })
      },
      fail(err){
        _this.setData({ isLoading: false })
        wx.showToast({
          title: "搜索企业失败:" + err.message,
          icon: 'none'
        })
      }
    })
  },
  toCompanyDetail(e){
    let pid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/HomePage/prisedetail/prisedetail?id='+ pid,
    })
  },
  getPositionList(){
    let params = {
      companySize: "",
      industry: "",
      nature: "",
      pageNum: this.data.search.pageNum,
      pageSize: this.data.search.pageSize,
      positionName: this.data.searchValue,
      userUid: "",
      wageRange: "",
    }
    this.setData({ isLoading: true })
    let _this = this
    wx.request({
      url: url+ 'position/findWorkList',
      method: "POST",
      header:{ 'Content-Type': 'application/json',},
      data: params,
      success(res){
        _this.setData({ isLoading: false })
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: resData.msg || "搜索职位出错",
            icon:'error'
          })
          return
        }
        let dataList = (resData.data.content || []).filter(item => item.id) 
        
        if(_this.data.search.pageNum > 1 && dataList.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        _this.setData({
          dataList: [..._this.data.dataList, ...dataList]
        })
      },
      fail(err){
        _this.setData({ isLoading: false })
        wx.showToast({
          title: "搜索职位失败："+ err.message,
          icon: 'none'
        })
      }
    })
  },
  toPositionDetail(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/HomePage/workdetail/workdetail?id='+ id,
    })
  },
  getRCList(){
    let params = {
      ageBegin: "",
      ageEnd: "",
      education: "",
      experienceBegin: "",
      experienceEnd: "",
      pageNum: this.data.search.pageNum,
      pageSize: this.data.search.pageSize,
      name: this.data.searchValue,
      sex: "",
      specialty: "",
    }
    this.setData({ isLoading: true })
    let _this = this
    wx.request({
      url: url+ 'findPeople/getFindPeopleList',
      method: "POST",
      header:{ 'Content-Type': 'application/json',},
      data: params,
      success(res){
        _this.setData({ isLoading: false })
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: resData.msg || "搜索人才出错",
            icon:'error'
          })
          return
        }
        let dataList = (resData.data.content || []).filter(item => item.id) 
        
        if(_this.data.search.pageNum > 1 && dataList.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        _this.setData({
          dataList: [..._this.data.dataList, ...dataList]
        })
      },
      fail(err){
        _this.setData({ isLoading: false })
        wx.showToast({
          title: "搜索人才出错: "+ err.message,
          icon: 'none'
        })
      }
    })
  },
  toJianLi(e){
    let { item } = e.currentTarget.dataset
    wx.setStorageSync('jl_header', item.header)
    wx.navigateTo({
      url: '/HomePage/linejianli/linejianli?type=view&uid='+ item.userUid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.search.pageNum++
    this.setData({
      search: this.data.search
    })
    this.onSearch()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})